<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2020 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://demo.thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/ThinkAdmin
// | github 代码仓库：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\index\controller;

use think\admin\Controller;


/**
 * Class Aboutus
 * @package app\index\controller
 */
class Aboutus extends BaseController
{

    public static $category_id = 6; //企业简介
    static public $limit = 6;
    public $table = "CmsArticles";

    public function index()
    {
        $this->title = '关于协会组织';

        //单页信息
        $sub_info_category = $this->_get_sub_category(self::$category_id,"info");
        for($i=0; $i<count($sub_info_category); $i++){
            $sub_info_category[$i]['article'] = $this->_get_category_articles($sub_info_category[$i]['id'],0,1);
        }
        //列表信息
        $sub_infolist_category = $this->_get_sub_category(self::$category_id,"infolist");
        for($i=0; $i<count($sub_infolist_category); $i++){
            $sub_infolist_category[$i]['articles'] = $this->_get_category_articles($sub_infolist_category[$i]['id'],0,10);
        }

        //协会动态信息
        $sub_imglist_category = $this->_get_sub_category(self::$category_id,"infoimg");
        for($i=0; $i<count($sub_imglist_category); $i++){
            $sub_imglist_category[$i]['articles'] = $this->_get_category_articles($sub_imglist_category[$i]['id'],0,10);
        }

        $this->aboutus = array(
            "info" => $this->_get_category(self::$category_id),
            "pageinfo" => $sub_info_category,
            "listinfo" => $sub_infolist_category,
            "infoimg" => $sub_imglist_category,
        );

        $this->_get_nav_menus();
        $this->_system();
        $this->fetch();
    }

    public function list(){

        $this->id = $this->request->get("id");
        $category = $this->_get_sub_category(self::$category_id);
        $selected = $this->id?$this->id:$category[0]['id'];

        $this->kw = $this->intro = $this->request->get("kw");
        $this->page = intval($this->request->get("page",1));
        $pager = $this->_prepare_page_info($selected,self::$limit,$this->kw);
        $offset = ($this->page-1)*self::$limit;

        $map = array("is_deleted"=>"0", "category_id"=>$selected);
        $query = $this->app->db->name($this->table)->where($map)
            ->whereLike("title|content","%".$this->kw."%");
        $list = $query->order("id desc")->limit($offset,self::$limit)->select()->toArray();

        $this->aboutus = array(
            "info" => $this->_get_category($selected), //栏目
            "category" => $category,//获取栏目下所有分类
            "selected" => $this->_get_category($selected),//获取栏目单个子栏目
            "list" => $list,
            "pager" => $pager
        );
        $this->title = $this->aboutus['info']['title'];
        $this->_get_nav_menus();
        $this->_system();
        $this->fetch();
    }

    public function detail()
    {

        $id = $this->request->get("id");
        $this->article = $this->_get_article($id);
        $this->category = $this->_get_category($this->article['category_id']);
        $this->_get_nav_menus();
        $this->_system();

        if($this->category['is_private']==1 && session('member')==null){
            $this->fetch("common/accessDenied");
        }else{
            $this->_inc_read_num($id);
            $this->title = $this->article['title'];
            $this->fetch("common/detail");
        }
    }

    public function _get_aboutus_info($id){
        $map = array("c.id" => $id, "c.status"=>1);
        $Model= $this->app->db->name('CmsArticles');
        return $Model ->alias("a")
            ->field("a.*")
            ->join('cms_category c', "c.id=a.category_id")
            ->where($map)
            ->order('a.sort desc,a.id desc')
            ->find();
    }

    public function _get_aboutus_category($category_id){

       $parent = $this->app->db->name('CmsCategory')->field("id,parent_id")
                    ->where(["id"=>$category_id])->find();

        $map = array("status"=>1,"parent_id"=>$parent['parent_id'],"type"=>"info");
        return $this->app->db->name('CmsCategory')->field("id,title")->where($map)->select();
    }


}